package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.repo.IBrandRedisRepository;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理品牌业务实现类
 *
 * @author liuweitao
 * @version 1.0.0
 */
@Service
@Slf4j
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private IBrandRedisRepository repository;

    @Override
    public void addNew(BrandAddNewDTO brandAddNewDTO) {
        log.debug("开始处理添加品牌的业务，参数：{}",brandAddNewDTO);
        String name = brandAddNewDTO.getName();
        int result = brandMapper.countByName(name);
        if (result != 0){
            String message = "添加品牌失败，名称已存在，请更换其它的名称";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Brand brand = new Brand();
        BeanUtils.copyProperties(brandAddNewDTO,brand);
        brandMapper.insert(brand);
        log.debug("添加品牌成功");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除品牌的业务，参数：{}",id);
        BrandStandardVO brandStandardVO = brandMapper.getStandardById(id);
        if (brandStandardVO == null) {
            String message = "删除品牌失败，数据不存在，请更换其它的id";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        int rows = brandMapper.deleteById(id);
        if(rows != 1){
            String message = "删除品牌失败，服务器忙，请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        log.debug("删除品牌成功");
    }

    @Override
    public void setEnable(Long id) {
        this.updateEnableById(id,1);
    }

    @Override
    public void setDisable(Long id) {
        this.updateEnableById(id,0);
    }

    //检查：id是否存在；状态是否冲突
    private void updateEnableById(Long id,Integer enable){
        String[] tips = {"禁用","启用"};
        log.debug("开始处理" + tips[enable] + "【id=" + id + "的品牌】业务");
        BrandStandardVO queryResult = brandMapper.getStandardById(id);
        if(queryResult == null){
            String message = tips[enable] + "品牌失败，数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        if(enable.equals(queryResult.getEnable())){
            String message = tips[enable] + "品牌失败，该品牌已存于" + tips[enable] + "状态";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Brand brand = new Brand();
        brand.setId(id).setEnable(enable);
        log.debug("即将" + tips[enable] + "品牌");
        int rows = brandMapper.updateById(brand);
        if(rows != 1){
            String message = tips[enable] + "品牌失败，服务器忙，请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        log.debug(tips[enable] + "品牌完成，受影响行数：{}",rows);
    }

    @Override
    public BrandStandardVO getStandardById(Long id) {
        log.debug("开始处理查询品牌详情的业务，参数id:{}",id);
//        BrandStandardVO brandStandardVO = brandMapper.getStandardById(id);
        BrandStandardVO brandStandardVO = repository.get(id);
        if(brandStandardVO == null){
            String message = "查询失败，品牌id不存在，请更换其它id";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return brandStandardVO;
    }

    @Override
    public List<BrandListItemVO> list() {
        log.debug("开始处理查询品牌业务");
//        return brandMapper.list();
        return repository.list();
    }

    @Override
    public void rebuildCache() {
        log.debug("从Redis删除所有的品牌数据");
        repository.deleteAll();

        log.debug("从MySQL中读取品牌列表");
        List<BrandListItemVO> brands = brandMapper.list();

        log.debug("将品牌列表写入到Redis中");
        repository.save(brands);

        log.debug("逐个根据id从MySQL中读取品牌详情，并写入到Redis");
        for (BrandListItemVO item : brands) {
            BrandStandardVO brand = brandMapper.getStandardById(item.getId());
            repository.save(brand);
        }
    }
}
